﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;

using ECCIT.Extension.Weibo_v1.Common;
using ECCIT.Extension.Weibo_v1.Model;
using ECCIT.Extension.Weibo_v1.Lib;
using ECCIT.Extension.Weibo_v2.Common;
using ECCIT.Extension.Weibo_v2.Configuration;
using ECCIT.Extension.Weibo_v2.Lib;
using OgilvyOne.Sharan.Model;
using OgilvyOne.Sharan.BLL;

namespace OgilvyOne.Sharan.Web.Weibo
{
    using Environment_v2 = ECCIT.Extension.Weibo_v2.Configuration.Environment;
    using OAuthAccessToken_v2 = ECCIT.Extension.Weibo_v2.Common.OAuthAccessToken;
    using Microblog_v2 = ECCIT.Extension.Weibo_v2.Lib.Microblog;
    using UserInfo_v2 = ECCIT.Extension.Weibo_v2.Model.UserInfo;

    public partial class XmlHttpGate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/xml";
            Response.ContentEncoding = Encoding.UTF8;

            try
            {
                string operation = Request.Params["op"];
                string output = null;
                WeiboUserBLL bll = new WeiboUserBLL();

                if (operation == null)
                {
                    string statusTag = ConfigCacheManager.StatusTag;
                    output = bll.GetUserLatestStatusAboutSharan(statusTag);
                }
                else if (operation == "1")
                {
                    string result = "<result>{0}</result>";
                    if (Session["WeiboUserID"] == null)
                    {
                        output = string.Format(result, "0");
                    }
                    else
                    {
                        DateTime? dt = (DateTime?)Application["ExpireTime"];
                        if (dt.HasValue)
                        {
                            if (dt.Value.Subtract(DateTime.Now).TotalSeconds < 86000)
                                output = string.Format(result, "1");
                            else
                                output = string.Format(result, "0");
                        }
                    }
                }
                else if (operation == "2")
                {
                    object o = Request.QueryString["access_token"];
                    string accessToken = null;
                    string sUserID = null;
                    long userID = 0;
                    bool isLogin = false;

                    if (o == null)
                    {
                        if (Session["WeiboUserID"] == null)
                        {
                            isLogin = false;
                        }
                        else
                        {
                            isLogin = true;
                            sUserID = (string)Session["WeiboUserID"];
                            accessToken = (string)Application["access_token"];
                        }
                    }
                    else
                    {
                        accessToken = (string)o;
                        Application["ExpireTime"] = DateTime.Now.AddSeconds(86200);
                        Application["access_token"] = accessToken;
                        sUserID = Request.Params["uid"];
                        Session["WeiboUserID"] = sUserID;
                        isLogin = true;
                    }

                    if (isLogin)
                    {
                        userID = long.Parse(sUserID);
                        OAuthAccessToken_v2 token = new OAuthAccessToken_v2();
                        token.Token = accessToken;
                        Environment_v2.AccessToken = token;

                        Environment_v2.AccessToken.UserID = sUserID;
                        output = bll.GetActiveFollowerStatuses(userID);
                    }
                    else
                    {
                        output = "<result>false</result>";
                    }
                }
                else if (operation == "3")
                {
                    output = "<root>";
                    if (Application["access_token"] != null)
                        output += (string)Application["access_token"];

                    if (Application["ExpireTime"] != null)
                        output += "_" + ((DateTime)Application["ExpireTime"]).ToString("yyyy-MM-dd HH:mm:ss");

                    DateTime? dt = (DateTime?)Application["ExpireTime"];
                    if (dt.HasValue)
                    {
                        output += "_" + dt.Value.Subtract(DateTime.Now).TotalSeconds;
                    }

                    output += "</root>";
                }

                Response.Write(output);
            }
            catch (Exception ex)
            {
                Response.Write("<Error><![CDATA[Error occurred in XmlHttpGate. Phase: 1. Message: " + ex.Message + "]]></Error>");
            }
        }
    }
}